package com.nbcio.modules.crm.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.customer.domain.CrmCustomerCategory;
import com.nbcio.modules.crm.customer.service.ICrmCustomerCategoryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM客户分类管理控制器
 */
@Tag(name = "CRM客户分类管理")
@RestController
@RequestMapping("/crm/customer/category")
@RequiredArgsConstructor
public class CrmCustomerCategoryController {

    private final ICrmCustomerCategoryService categoryService;

    /**
     * 分页查询客户分类
     */
    @GetMapping("/page")
    public R<IPage<CrmCustomerCategory>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String categoryCode,
            @RequestParam(required = false) String categoryName,
            @RequestParam(required = false) Long parentId,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<CrmCustomerCategory> qw = new LambdaQueryWrapper<>();
        if (categoryCode != null && !categoryCode.isEmpty()) {
            qw.like(CrmCustomerCategory::getCategoryCode, categoryCode);
        }
        if (categoryName != null && !categoryName.isEmpty()) {
            qw.like(CrmCustomerCategory::getCategoryName, categoryName);
        }
        if (parentId != null) {
            qw.eq(CrmCustomerCategory::getParentId, parentId);
        } else {
            qw.isNull(CrmCustomerCategory::getParentId);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmCustomerCategory::getStatus, Integer.parseInt(status));
        }
        qw.orderByAsc(CrmCustomerCategory::getSortOrder);
        IPage<CrmCustomerCategory> page = categoryService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 查询所有客户分类（树形结构）
     */
    @GetMapping("/tree")
    public R<List<CrmCustomerCategory>> tree(@RequestParam(required = false) String status) {
        LambdaQueryWrapper<CrmCustomerCategory> qw = new LambdaQueryWrapper<>();
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmCustomerCategory::getStatus, Integer.parseInt(status));
        }
        qw.orderByAsc(CrmCustomerCategory::getSortOrder);
        List<CrmCustomerCategory> list = categoryService.list(qw);
        return R.ok(list);
    }

    /**
     * 根据ID查询客户分类
     */
    @GetMapping("/{id}")
    public R<CrmCustomerCategory> get(@PathVariable("id") String id) {
        CrmCustomerCategory category = categoryService.getById(Long.parseLong(id));
        return R.ok(category);
    }

    /**
     * 新增客户分类
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmCustomerCategory category) {
        return R.ok(categoryService.save(category));
    }

    /**
     * 更新客户分类
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmCustomerCategory category) {
        category.setCategoryId(Long.parseLong(id));
        return R.ok(categoryService.updateById(category));
    }

    /**
     * 删除客户分类
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(categoryService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除客户分类
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(categoryService.removeByIds(longIds));
    }
}

